﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToTwitter;

namespace Twitter
{
    public partial class Index : System.Web.UI.Page
    {
        public String userId
        {
            set;
            get;
        }

        public String tweetId
        {
            set;
            get;
        }

        public LinqToTwitter.User user
        {
            set
            {

            }
            get
            {
                if ((Session["auth"] as WebAuthorizer).ScreenName == null && Session["registerUserName"]==null)
                    return null;
                Util.UserTweets util = new Util.UserTweets(Request, Response, userId);
                String name = Session["registerUserName"]!=null? Session["registerUserName"] as String:(Session["auth"] as WebAuthorizer).ScreenName;
                return util.getUserByName(Session["auth"] as WebAuthorizer, name);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Util.Authentification authen = new Util.Authentification();
                Boolean b = authen.isAuthentificated(Request, Response);

                if (!b)
                {
                    Session["auth"] = null;
                    Session["registerUserId"] = null;
                    authen.auth.BeginAuthorization(Request.Url);
                    Session["auth"] = authen.auth;
                    Session["registerUserId"] = authen.auth.UserId;
                }
                else
                {
                    if (((String)Session["registerUserId"]) == null || "".Equals(((String)Session["registerUserId"])))
                    {
                        Session["registerUserId"] = authen.auth.UserId;
                        Session["registerUserName"] = authen.auth.ScreenName;
                    }

                    if (Session["auth"] == null)
                    {
                        Session["auth"] = authen.auth;
                    }

                }

                userId = Session["registerUserId"] as String;


                if (Request.Params["delete"] != null && Request.Params["delete"] != "")
                {
                    doDeletetWEET(Request.Params["delete"]);
                }

                if (Request.Params["retweet"] != null && Request.Params["retweet"] != "")
                {
                    doRetweet(Request.Params["retweet"]);
                }

                if (Request.Params["modifier"] != null && Request.Params["modifier"] != "")
                {
                    doDeletetWEET(Request.Params["modifier"]);
                    doModify(Request.Params["text"]);
                }
            }
        }


        public List<Status> getUserTweets()
        {
            Util.UserTweets usrTweets = new Util.UserTweets(Request, Response, userId);
            return usrTweets.getTweets(Session["auth"] as WebAuthorizer);
        }

        protected void doTweetClicked(object sender, EventArgs e)
        {
            Util.UserTweets usrTweets = new Util.UserTweets(Request, Response, userId);
            //usrTweets.doTweet(usrTweets.getTweet(Session["auth"] as WebAuthorizer, tweetId).Text, Session["auth"] as WebAuthorizer);
            usrTweets.doTweet(txtTweet.Text, Session["auth"] as WebAuthorizer);
        }


        protected void doRetweet(String tweetId)
        {
         Util.UserTweets usrTweets = new Util.UserTweets(Request, Response, userId);
         usrTweets.doTweet(usrTweets.getTweet(Session["auth"] as WebAuthorizer, tweetId).Text, Session["auth"] as WebAuthorizer);
        }

        protected void doModify(String tweet)
        {
            this.txtTweet.Text = tweet;
        }

        protected void doDeletetWEET(String tweetId)
        {
            try
            {
                Util.UserTweets tweets = new Util.UserTweets(Request, Response, userId);
                tweets.deleteTweet(Session["auth"] as WebAuthorizer, tweetId);
            }
            catch (Exception ex)
            { }
        }

        protected void reloadTweets(object sender, EventArgs e)
        {
            UpdatePanel1.Update();
        }

    }
}